package self;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.ZParams;

import java.util.*;

/**
 * Created by 刘万祥 on 2017/5/3 0003.
 * 文章信息表
 * 文章评分表
 * 文章发布时间表
 */
public class Chapter01 {

    private static final double SCORE = 432;
    public static final int ONE_WEEK = 7 * 86400;

    public static void main(String[] args) {
        Jedis conn = new Jedis("localhost");
        conn.select(15);

//       创建一个文章信息表
        String articleId= postArticle(conn);
        String articlePrefix = "article:";
        String article = articlePrefix + articleId;

        Map<String, String> articleMap = conn.hgetAll(article);

//      投票
        articleVote(conn, articleId, article);

//        找出前10个分数最高的文章
        highestScoreArticles(conn);

//        把文章添加到指定的组里面
        String[] groups = {"1", "2"};

        for (String group : groups) {
            conn.sadd("group:" + group, article);
        }
//        获取该组得分最高的10篇文章.因为添加组的时候,没有添加把该文章的分数也添加上.这里需要做个交集
        String scoreKey = "score:";
        String key = scoreKey + "1";
        if (!conn.exists(key)) {
            ZParams params=new ZParams().aggregate(ZParams.Aggregate.MAX);
            conn.zinterstore(key, params, scoreKey, "group:" + "1");
            conn.expire(key, 60);
        }


    }

    private static void highestScoreArticles(Jedis conn) {
        String scoreKey = "score:";
        Set<String> scores = conn.zrevrange(scoreKey, 0, 10);
        List<Map<String, String>> articles = new ArrayList<>();
        for (String id : scores) {
            Map<String, String> articleData = conn.hgetAll(id);
            articles.add(articleData);
        }
    }

    private static void articleVote(Jedis conn, String articleId, String article) {
        long cutoff=System.currentTimeMillis()-ONE_WEEK*1000;
        String timeKey = "time:";
        if (conn.zscore(timeKey, article) >= cutoff) {

            String otherUser = "万事吉祥";
            String votedKey = "voted:"+articleId;
//            如果==1,说明添加用户成功,分数表和文章信息表的相应字段也做出更新
            if (conn.sadd(votedKey, otherUser) == 1) {
                String scoreKey = "score:";
//            每次投票都加 score 的分数
                conn.zincrby(scoreKey, SCORE, article);

                conn.hincrBy(article, "votes", 1);
            }


        }
    }

    private static String  postArticle(Jedis conn) {
        String articleId = conn.incr("article:")+"";

        String article = "article:" + articleId;
        Map<String, String> articleData = new HashMap<>();
        articleData.put("title", "标题1");
        articleData.put("link", "http://www.baidu.com");
        articleData.put("poster", "刘万祥");
        articleData.put("time", System.currentTimeMillis() + "");
        articleData.put("votes", "1");

        conn.hmset(article, articleData);

        String timeKey = "time:";
        conn.zadd(timeKey, System.currentTimeMillis(), article);

        String scoreKey = "score:";
        conn.zadd(scoreKey, System.currentTimeMillis()+ SCORE, article);

        String currentUser = "刘万祥";
//        记录已经投过票的用户,这个不需要有序,所以用一个集合
        String votedKey = "voted:"+articleId;
        conn.sadd(votedKey, currentUser);
//        因为文章只能在一个周之内投票,所以该文章的缓存时间,保留一个周
        conn.expire(votedKey, ONE_WEEK);

        return articleId;
    }




}
